package com.iwhalecloud.bss.kite.cucc.service.attr;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IChargeUnitService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.ChargeUnitDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.ChargeUnitQryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.vo.attr.ChargeUnitVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.attr.UnicomRegionTreeVO;
import com.iwhalecloud.bss.kite.cucc.service.cache.ChargeUnitCache;
import com.iwhalecloud.bss.kite.cucc.service.cache.PlatformRegionCache;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChargeUnitService implements IChargeUnitService {

    /**
     * 收费单位查询
     * @author zhang.song
     * @date 2022-01-19 11:47
     * @param chargeUnitQryDTO
     * @return com.iwhalecloud.bss.kite.cucc.client.vo.attr.ChargeUnitVO
     */
    @Override
    public ChargeUnitVO queryChargeUnit(ChargeUnitQryDTO chargeUnitQryDTO) {
        List<ChargeUnitDTO> chargeUnits = ChargeUnitCache.get(chargeUnitQryDTO);
        ChargeUnitVO chargeUnitVO = new ChargeUnitVO();
        chargeUnitVO.setChargeUnits(chargeUnits);
        chargeUnitVO.setCode("00000");
        return chargeUnitVO;
    }

    /**
     * 查询区域省市树
     * @author zhang.song
     * @date 2022-02-06 16:25
     * @return java.util.List<com.iwhalecloud.bss.kite.cucc.client.vo.attr.UnicomRegionTreeVO>
     */
    @Override
    public List<UnicomRegionTreeVO> getProvCityTree() {
        List<UnicomRegionTreeVO> regionTrees = new ArrayList<>();
        List<UnicomRegion> provList = PlatformRegionCache.get("10", null);
        if(KiteListUtils.isNotEmpty(provList)) {
            List<UnicomRegion> cityList = PlatformRegionCache.get("20", null);
            provList.forEach(prov -> {
                UnicomRegionTreeVO unicomRegionTreeVO = new UnicomRegionTreeVO(prov);
                List<UnicomRegion> subRegionList = cityList.stream().filter(city -> KiteStringUtils.isEqual(prov.getRegionCode(), city.getParRegionCode())).collect(Collectors.toList());
                unicomRegionTreeVO.setSubRegionList(subRegionList);
                regionTrees.add(unicomRegionTreeVO);
            });
        }
        return regionTrees;
    }
}
